//
//  HHTableViewController.swift
//  HHSwift
//
//  Created by 华宏 on 2019/1/22.
//  Copyright © 2019年 huahong. All rights reserved.
//

import UIKit
import RxSwift

class BasicListController: BaseViewController,UITableViewDelegate {
    
    var tableView :UITableView!
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "语法"

        self.tableView = UITableView(frame: self.view.bounds, style: .plain)
        self.view.addSubview(self.tableView)
        tableView.delegate = self
        self.tableView.backgroundColor = .white
        self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "reuseIdentifier")
        
        //绑定cell && numberOfRowsInSection
//        self.dataArray.bind(to: tableView.rx.items) { (tableView, row, element) in
//            let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier")!
//            cell.textLabel?.text = "\(element)"
//            return cell
//        }.disposed(by: disposeBag)
        
        self.dataArray.bind(to: tableView.rx.items(cellIdentifier: "reuseIdentifier")){
            (row,element,cell) in
            
            cell.accessoryType = .disclosureIndicator
            cell.textLabel?.text = "\(row + 1): \(element)"
           
        }.disposed(by: disposeBag)
        
//        //点击事件
//        self.tableView.rx.itemSelected.subscribe(onNext: {[weak self] (indexPath) in
//            //调用下面的点击方法
//            self?.tableView((self?.tableView)!, didSelectRowAt: indexPath)
//        }).disposed(by: disposeBag)
        
        self.tableView.rx.modelSelected(String.self).subscribe(onNext: { (item) in
            print("\(item) 被点击了")
        }).disposed(by: disposeBag)

        //删除cell
        self.tableView.rx.itemDeleted.subscribe(onNext: { (indexPath) in
            print("\(indexPath)被移除")
        }).disposed(by: disposeBag)
        
        self.tableView.rx.modelDeleted(String.self).subscribe(onNext: { (item) in
            print("\(item)被移除")
        }).disposed(by: disposeBag)
        
        //移动cell
//        tableView.setEditing(true, animated: true)
        
        tableView.rx.itemMoved.subscribe(onNext: { (arg0) in
            
            let (sourceIndex, destinationIndex) = arg0
            print("sourceIndex:\(sourceIndex),destinationIndex:\(destinationIndex)")
        }).disposed(by: disposeBag)
        
        //插入cell
        tableView.rx.itemInserted.subscribe(onNext: { (indexPath) in
            print("\(indexPath)Insert")
        }).disposed(by: disposeBag)
        
        //willDisplayCell
        tableView.rx.willDisplayCell.subscribe(onNext: { (arg0) in
            
//            let (cell, indexPath) = arg0
//            print("indexPath:\(indexPath),cell:\(cell)")
        }).disposed(by: disposeBag)
        
       
        
    }

    
    // MARK: - Table view data source
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .insert
    }
    
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        switch indexPath.row {
        case 0:
            
            let vc = BasicViewController()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        case 1:
       
            let vc = ClosuresController()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        case 2:

            let vc = FuncController()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        case 3:
            
            let vc = EnumController()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        case 4:
            
            let vc = StructController()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        case 5:
            
            let vc = ClassController()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        case 6:
            
            let vc = ProtocolController()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        case 7:
            
            let vc = ControlFlowController()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        case 8:
                   
           let vc = ExtensionController()
           vc.hidesBottomBarWhenPushed = true
           self.navigationController?.pushViewController(vc, animated: true)
        
        default: break
            
        }
    }
     

    lazy var dataArray = {
       
        return Observable.just(["基础类型","闭包","函数","枚举","结构体","类","协议","控制流","扩展"])
        
        
    }()
    
    
    
}
